package chances.epg.navigator.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import chances.epg.commons.utils.PathUtils;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.template.TemplateContext;

/**
 * ajax调用处理
 * 
 * @author chances
 *
 */
@Controller
public class AjaxReqController extends AccessController {

	@RequestMapping(value = "/{siteCode}/{bizCode}/ajax/{fileName}")
	public String ajaxControl(ModelMap model, 
			HttpServletRequest request,HttpServletResponse response,
			@PathVariable String fileName) throws IOException{
		AccessContext context = this.accessContextFactory.createAccessContext(request);
		TemplateContext templateContext = this.getBizTemplateContext(request, context);
		this.setAccessContext(model, request, context);
		this.setTemplateContext(model, request, templateContext,context);
		String path = templateContext.getTemplateFilePath();
		path = PathUtils.getPath(path);
		path = PathUtils.join(path,"ajax",fileName+".jsp");
		
		return path;
	}
	
	@RequestMapping(value = "/{siteCode}/{bizCode}/{categoryCode}/ajax/{fileName}")
	public String ajaxCategoryControl(ModelMap model, 
			HttpServletRequest request,HttpServletResponse response,
			@PathVariable String fileName) throws IOException{
		return this.ajaxControl(model, request, response, fileName);
	}
	
}
